package LC;

/**
 * https://leetcode.com/problems/bitwise-and-of-numbers-range/description/
 * Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
 * For example, given the range [5, 7], you should return 4.
 */
public class LC_201_BitwiseANDofNumbersRange_BitManipulation {
    public static void main(String[] args) {
        System.out.println(Solution.rangeBitwiseAnd(10, 20));
    }

    static class Solution {
        static int rangeBitwiseAnd(int m, int n) {
            int bit = 0;
            while (m != n) {
                m >>= 1;
                n >>= 1;
                bit++;
            }
            return m << bit;
        }
    }
}